---
description: "Swift documentation for 'RangeReplaceableCollection'"
layout: "default"
title: "RangeReplaceableCollection"
keywords: ""
root: "/v5.1"
---

<div class="intro-declaration"><code class="language-swift">protocol RangeReplaceableCollection</code></div><p>A collection that supports replacement of an arbitrary subrange of elements
with the elements of another collection.</p>
<table class="standard"><tr><th id="inheritance">Inheritance</th><td><code class="inherits">Collection</code></td></tr><tr><th id="conforming-types">Conforming Types</th><td><code class="inherits">Array, ArraySlice, ContiguousArray, String, String.UnicodeScalarView, Substring, Substring.UnicodeScalarView</code></td></tr><tr><th id="associatedtypes">Associated Types</th><td><span id="aliasesmark"></span><div class="declaration"><code class="language-swift">associatedtype SubSequence</code><div class="comment"></div></div></td></tr></table><div class="discussion comment"><p>Range-replaceable collections provide operations that insert and remove
elements. For example, you can add elements to an array of strings by
calling any of the inserting or appending operations that the
<code>RangeReplaceableCollection</code> protocol defines.</p>
<pre><code class="language-swift">var bugs = [&quot;Aphid&quot;, &quot;Damselfly&quot;]
bugs.append(&quot;Earwig&quot;)
bugs.insert(contentsOf: [&quot;Bumblebee&quot;, &quot;Cicada&quot;], at: 1)
print(bugs)
// Prints &quot;[&quot;Aphid&quot;, &quot;Bumblebee&quot;, &quot;Cicada&quot;, &quot;Damselfly&quot;, &quot;Earwig&quot;]&quot;
</code></pre>
<p>Likewise, <code>RangeReplaceableCollection</code> types can remove one or more
elements using a single operation.</p>
<pre><code class="language-swift">bugs.removeLast()
bugs.removeSubrange(1...2)
print(bugs)
// Prints &quot;[&quot;Aphid&quot;, &quot;Damselfly&quot;]&quot;

bugs.removeAll()
print(bugs)
// Prints &quot;[]&quot;
</code></pre>
<p>Lastly, use the eponymous <code>replaceSubrange(_:with:)</code> method to replace
a subrange of elements with the contents of another collection. Here,
three elements in the middle of an array of integers are replaced by the
five elements of a <code>Repeated&lt;Int&gt;</code> instance.</p>
<pre><code class="language-swift"> var nums = [10, 20, 30, 40, 50]
 nums.replaceSubrange(1...3, with: repeatElement(1, count: 5))
 print(nums)
 // Prints &quot;[10, 1, 1, 1, 1, 1, 50]&quot;
</code></pre>
<h3>Conforming to the RangeReplaceableCollection Protocol</h3>
<p>To add <code>RangeReplaceableCollection</code> conformance to your custom collection,
add an empty initializer and the <code>replaceSubrange(_:with:)</code> method to your
custom type. <code>RangeReplaceableCollection</code> provides default implementations
of all its other methods using this initializer and method. For example,
the <code>removeSubrange(_:)</code> method is implemented by calling
<code>replaceSubrange(_:with:)</code> with an empty collection for the <code>newElements</code>
parameter. You can override any of the protocol's required methods to
provide your own custom implementation.</p>
</div><h3>Initializers</h3><div id="init-e67906ab4373125a18eb2b5a75f59bd2" class="declaration"><a class="toggle-link" href="#comment-init-e67906ab4373125a18eb2b5a75f59bd2">init init()</a> <span class="required">Required</span><div class="comment collapse in" id="comment-init-e67906ab4373125a18eb2b5a75f59bd2"><p>Creates a new, empty collection.</p>
<h4>Declaration</h4><code class="language-swift">init()</code></div></div><div id="init-00a2b987552997ee43208aca83ac6ae6" class="declaration"><a class="toggle-link" href="#comment-init-00a2b987552997ee43208aca83ac6ae6">init init(_:)</a> <span class="required">Required</span><div class="comment collapse in" id="comment-init-00a2b987552997ee43208aca83ac6ae6"><p>Creates a new instance of a collection containing the elements of a
sequence.</p>
<ul>
<li>Parameter elements: The sequence of elements for the new collection.
<code>elements</code> must be finite.</li>
</ul>
<h4>Declaration</h4><code class="language-swift">init&lt;S>(_ elements: S) where S: Sequence, Self.Element == S.Element</code></div></div><div id="init_repeating_count-386429a7befa4cc7818e4639b5637091" class="declaration"><a class="toggle-link" href="#comment-init_repeating_count-386429a7befa4cc7818e4639b5637091">init init(repeating:count:)</a> <span class="required">Required</span><div class="comment collapse in" id="comment-init_repeating_count-386429a7befa4cc7818e4639b5637091"><p>Creates a new collection containing the specified number of a single,
repeated value.</p>
<p>The following example creates an array initialized with five strings
containing the letter <em>Z</em>.</p>
<pre><code class="language-swift">let fiveZs = Array(repeating: &quot;Z&quot;, count: 5)
print(fiveZs)
// Prints &quot;[&quot;Z&quot;, &quot;Z&quot;, &quot;Z&quot;, &quot;Z&quot;, &quot;Z&quot;]&quot;
</code></pre>
<h4>Declaration</h4><code class="language-swift">init(repeating repeatedValue: Self.Element, count: Int)</code></div></div><h3>Subscripts</h3><div id="subscript_bounds-788086fb8ff4edc0aa2bd3c41f362493" class="declaration"><a class="toggle-link" href="#comment-subscript_bounds-788086fb8ff4edc0aa2bd3c41f362493">subscript subscript(bounds:)</a> <span class="required">Required</span><div class="comment collapse in" id="comment-subscript_bounds-788086fb8ff4edc0aa2bd3c41f362493"><h4>Declaration</h4><code class="language-swift">override subscript(bounds: Self.Index) -> Self.Element</code></div></div><div id="subscript_bounds-788086fb8ff4edc0aa2bd3c41f362493" class="declaration"><a class="toggle-link" href="#comment-subscript_bounds-788086fb8ff4edc0aa2bd3c41f362493">subscript subscript(bounds:)</a> <span class="required">Required</span><div class="comment collapse in" id="comment-subscript_bounds-788086fb8ff4edc0aa2bd3c41f362493"><h4>Declaration</h4><code class="language-swift">override subscript(bounds: Range&lt;Self.Index>) -> Self.SubSequence</code></div></div><h3>Instance Methods</h3><div id="append-1fd0af8599f8632e7a2669725979f22c" class="declaration"><a class="toggle-link" href="#comment-append-1fd0af8599f8632e7a2669725979f22c">func append(_ newElement: Self.Element)</a> <span class="required">Required</span><div class="comment collapse in" id="comment-append-1fd0af8599f8632e7a2669725979f22c"><p>Adds an element to the end of the collection.</p>
<p>If the collection does not have sufficient capacity for another element,
additional storage is allocated before appending <code>newElement</code>. The
following example adds a new number to an array of integers:</p>
<pre><code class="language-swift">var numbers = [1, 2, 3, 4, 5]
numbers.append(100)

print(numbers)
// Prints &quot;[1, 2, 3, 4, 5, 100]&quot;
</code></pre>
<ul>
<li>Parameter newElement: The element to append to the collection.</li>
</ul>
<blockquote>
<p>Complexity:
O(1) on average, over many calls to <code>append(_:)</code> on the same collection.</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">mutating func append(_ newElement: Self.Element)</code></div></div><div id="append_contentsOf-ac3371fb120478d23360c9cd8a715233" class="declaration"><a class="toggle-link" href="#comment-append_contentsOf-ac3371fb120478d23360c9cd8a715233">func append(contentsOf newElements: S)</a> <span class="required">Required</span><div class="comment collapse in" id="comment-append_contentsOf-ac3371fb120478d23360c9cd8a715233"><p>Adds the elements of a sequence or collection to the end of this
collection.</p>
<p>The collection being appended to allocates any additional necessary
storage to hold the new elements.</p>
<p>The following example appends the elements of a <code>Range&lt;Int&gt;</code> instance to
an array of integers:</p>
<pre><code class="language-swift">var numbers = [1, 2, 3, 4, 5]
numbers.append(contentsOf: 10...15)
print(numbers)
// Prints &quot;[1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15]&quot;
</code></pre>
<ul>
<li>Parameter newElements: The elements to append to the collection.</li>
</ul>
<blockquote>
<p>Complexity:
O(<em>m</em>), where <em>m</em> is the length of <code>newElements</code>.</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">mutating func append&lt;S>(contentsOf newElements: S) where S: Sequence, Self.Element == S.Element</code></div></div><div id="insert_at-2422c5a8ae5e7107d8c851b9681eb071" class="declaration"><a class="toggle-link" href="#comment-insert_at-2422c5a8ae5e7107d8c851b9681eb071">func insert(_ newElement: Self.Element, at i: Self.Index)</a> <span class="required">Required</span><div class="comment collapse in" id="comment-insert_at-2422c5a8ae5e7107d8c851b9681eb071"><p>Inserts a new element into the collection at the specified position.</p>
<p>The new element is inserted before the element currently at the
specified index. If you pass the collection's <code>endIndex</code> property as
the <code>index</code> parameter, the new element is appended to the
collection.</p>
<pre><code class="language-swift">var numbers = [1, 2, 3, 4, 5]
numbers.insert(100, at: 3)
numbers.insert(200, at: numbers.endIndex)

print(numbers)
// Prints &quot;[1, 2, 3, 100, 4, 5, 200]&quot;
</code></pre>
<p>Calling this method may invalidate any existing indices for use with this
collection.</p>
<ul>
<li>Parameter newElement: The new element to insert into the collection.</li>
</ul>
<ul>
<li>Parameter i: The position at which to insert the new element.
<code>index</code> must be a valid index into the collection.</li>
</ul>
<blockquote>
<p>Complexity:
O(<em>n</em>), where <em>n</em> is the length of the collection. If <code>i == endIndex</code>, this method is equivalent to <code>append(_:)</code>.</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">mutating func insert(_ newElement: Self.Element, at i: Self.Index)</code></div></div><div id="insert_contentsOf_at-c903317351c7d5642de7c18c61788233" class="declaration"><a class="toggle-link" href="#comment-insert_contentsOf_at-c903317351c7d5642de7c18c61788233">func insert(contentsOf newElements: S, at i: Self.Index)</a> <span class="required">Required</span><div class="comment collapse in" id="comment-insert_contentsOf_at-c903317351c7d5642de7c18c61788233"><p>Inserts the elements of a sequence into the collection at the specified
position.</p>
<p>The new elements are inserted before the element currently at the
specified index. If you pass the collection's <code>endIndex</code> property as the
<code>index</code> parameter, the new elements are appended to the collection.</p>
<p>Here's an example of inserting a range of integers into an array of the
same type:</p>
<pre><code class="language-swift">var numbers = [1, 2, 3, 4, 5]
numbers.insert(contentsOf: 100...103, at: 3)
print(numbers)
// Prints &quot;[1, 2, 3, 100, 101, 102, 103, 4, 5]&quot;
</code></pre>
<p>Calling this method may invalidate any existing indices for use with this
collection.</p>
<ul>
<li>Parameter newElements: The new elements to insert into the collection.</li>
</ul>
<ul>
<li>Parameter i: The position at which to insert the new elements. <code>index</code>
must be a valid index of the collection.</li>
</ul>
<blockquote>
<p>Complexity:
O(<em>n</em> + <em>m</em>), where <em>n</em> is length of this collection and <em>m</em> is the length of <code>newElements</code>. If <code>i == endIndex</code>, this method is equivalent to <code>append(contentsOf:)</code>.</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">mutating func insert&lt;S>(contentsOf newElements: S, at i: Self.Index) where S: Collection, Self.Element == S.Element</code></div></div><div id="remove_at-3e0c9900dc44c20e5d46b7cd3b677435" class="declaration"><a class="toggle-link" href="#comment-remove_at-3e0c9900dc44c20e5d46b7cd3b677435">func remove(at i: Self.Index) -> Self.Element</a> <span class="required">Required</span><div class="comment collapse in" id="comment-remove_at-3e0c9900dc44c20e5d46b7cd3b677435"><p>Removes and returns the element at the specified position.</p>
<p>All the elements following the specified position are moved to close the
gap. This example removes the middle element from an array of
measurements.</p>
<pre><code class="language-swift">var measurements = [1.2, 1.5, 2.9, 1.2, 1.6]
let removed = measurements.remove(at: 2)
print(measurements)
// Prints &quot;[1.2, 1.5, 1.2, 1.6]&quot;
</code></pre>
<p>Calling this method may invalidate any existing indices for use with this
collection.</p>
<ul>
<li>Parameter i: The position of the element to remove. <code>index</code> must be
a valid index of the collection that is not equal to the collection's
end index.</li>
</ul>
<blockquote>
<p>Complexity:
O(<em>n</em>), where <em>n</em> is the length of the collection.</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">mutating func remove(at i: Self.Index) -> Self.Element</code></div></div><div id="removeAll_keepingCapacity-258ecaf1c68e3810fad8df6f24bcd010" class="declaration"><a class="toggle-link" href="#comment-removeAll_keepingCapacity-258ecaf1c68e3810fad8df6f24bcd010">func removeAll(keepingCapacity keepCapacity: Bool)</a> <span class="required">Required</span><div class="comment collapse in" id="comment-removeAll_keepingCapacity-258ecaf1c68e3810fad8df6f24bcd010"><p>Removes all elements from the collection.</p>
<p>Calling this method may invalidate any existing indices for use with this
collection.</p>
<ul>
<li>Parameter keepCapacity: Pass <code>true</code> to request that the collection
avoid releasing its storage. Retaining the collection's storage can
be a useful optimization when you're planning to grow the collection
again. The default value is <code>false</code>.</li>
</ul>
<blockquote>
<p>Complexity:
O(<em>n</em>), where <em>n</em> is the length of the collection.</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">mutating func removeAll(keepingCapacity keepCapacity: Bool)</code></div></div><div id="removeAll_where-78f1ec47693ca0c39027cef2bf1c64be" class="declaration"><a class="toggle-link" href="#comment-removeAll_where-78f1ec47693ca0c39027cef2bf1c64be">func removeAll(where shouldBeRemoved: (Self.Element) throws -> Bool) rethrows</a> <span class="required">Required</span><div class="comment collapse in" id="comment-removeAll_where-78f1ec47693ca0c39027cef2bf1c64be"><p>Removes all the elements that satisfy the given predicate.</p>
<p>Use this method to remove every element in a collection that meets
particular criteria. The order of the remaining elements is preserved.
This example removes all the odd values from an
array of numbers:</p>
<pre><code class="language-swift">var numbers = [5, 6, 7, 8, 9, 10, 11]
numbers.removeAll(where: { $0 % 2 != 0 })
// numbers == [6, 8, 10]
</code></pre>
<ul>
<li>Parameter shouldBeRemoved: A closure that takes an element of the
sequence as its argument and returns a Boolean value indicating
whether the element should be removed from the collection.</li>
</ul>
<blockquote>
<p>Complexity:
O(<em>n</em>), where <em>n</em> is the length of the collection.</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">mutating func removeAll(where shouldBeRemoved: (Self.Element) throws -> Bool) rethrows</code></div></div><div id="removeFirst-90bb9af594748b627d638a3b28d286c7" class="declaration"><a class="toggle-link" href="#comment-removeFirst-90bb9af594748b627d638a3b28d286c7">func removeFirst() -> Self.Element</a> <span class="required">Required</span><div class="comment collapse in" id="comment-removeFirst-90bb9af594748b627d638a3b28d286c7"><p>Removes and returns the first element of the collection.</p>
<p>The collection must not be empty.</p>
<pre><code class="language-swift">var bugs = [&quot;Aphid&quot;, &quot;Bumblebee&quot;, &quot;Cicada&quot;, &quot;Damselfly&quot;, &quot;Earwig&quot;]
bugs.removeFirst()
print(bugs)
// Prints &quot;[&quot;Bumblebee&quot;, &quot;Cicada&quot;, &quot;Damselfly&quot;, &quot;Earwig&quot;]&quot;
</code></pre>
<p>Calling this method may invalidate any existing indices for use with this
collection.</p>
<blockquote>
<p>Complexity:
O(<em>n</em>), where <em>n</em> is the length of the collection.</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">mutating func removeFirst() -> Self.Element</code></div></div><div id="removeFirst-38c26d3d2ce7e1284c4720b69bb07b0c" class="declaration"><a class="toggle-link" href="#comment-removeFirst-38c26d3d2ce7e1284c4720b69bb07b0c">func removeFirst(_ k: Int)</a> <span class="required">Required</span><div class="comment collapse in" id="comment-removeFirst-38c26d3d2ce7e1284c4720b69bb07b0c"><p>Removes the specified number of elements from the beginning of the
collection.</p>
<pre><code class="language-swift">var bugs = [&quot;Aphid&quot;, &quot;Bumblebee&quot;, &quot;Cicada&quot;, &quot;Damselfly&quot;, &quot;Earwig&quot;]
bugs.removeFirst(3)
print(bugs)
// Prints &quot;[&quot;Damselfly&quot;, &quot;Earwig&quot;]&quot;
</code></pre>
<p>Calling this method may invalidate any existing indices for use with this
collection.</p>
<ul>
<li>Parameter k: The number of elements to remove from the collection.
<code>k</code> must be greater than or equal to zero and must not exceed the
number of elements in the collection.</li>
</ul>
<blockquote>
<p>Complexity:
O(<em>n</em>), where <em>n</em> is the length of the collection.</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">mutating func removeFirst(_ k: Int)</code></div></div><div id="removeSubrange-879999614b8060026a48a1f66c30ac8f" class="declaration"><a class="toggle-link" href="#comment-removeSubrange-879999614b8060026a48a1f66c30ac8f">func removeSubrange(_ bounds: Range&lt;Self.Index>)</a> <span class="required">Required</span><div class="comment collapse in" id="comment-removeSubrange-879999614b8060026a48a1f66c30ac8f"><p>Removes the specified subrange of elements from the collection.</p>
<pre><code class="language-swift">var bugs = [&quot;Aphid&quot;, &quot;Bumblebee&quot;, &quot;Cicada&quot;, &quot;Damselfly&quot;, &quot;Earwig&quot;]
bugs.removeSubrange(1...3)
print(bugs)
// Prints &quot;[&quot;Aphid&quot;, &quot;Earwig&quot;]&quot;
</code></pre>
<p>Calling this method may invalidate any existing indices for use with this
collection.</p>
<ul>
<li>Parameter bounds: The subrange of the collection to remove. The bounds
of the range must be valid indices of the collection.</li>
</ul>
<blockquote>
<p>Complexity:
O(<em>n</em>), where <em>n</em> is the length of the collection.</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">mutating func removeSubrange(_ bounds: Range&lt;Self.Index>)</code></div></div><div id="replaceSubrange_with-37f24f980773f1736abe2f2109e3c177" class="declaration"><a class="toggle-link" href="#comment-replaceSubrange_with-37f24f980773f1736abe2f2109e3c177">func replaceSubrange(_ subrange: Range&lt;Self.Index>, with newElements: C)</a> <span class="required">Required</span><div class="comment collapse in" id="comment-replaceSubrange_with-37f24f980773f1736abe2f2109e3c177"><p>Replaces the specified subrange of elements with the given collection.</p>
<p>This method has the effect of removing the specified range of elements
from the collection and inserting the new elements at the same location.
The number of new elements need not match the number of elements being
removed.</p>
<p>In this example, three elements in the middle of an array of integers are
replaced by the five elements of a <code>Repeated&lt;Int&gt;</code> instance.</p>
<pre><code class="language-swift"> var nums = [10, 20, 30, 40, 50]
 nums.replaceSubrange(1...3, with: repeatElement(1, count: 5))
 print(nums)
 // Prints &quot;[10, 1, 1, 1, 1, 1, 50]&quot;
</code></pre>
<p>If you pass a zero-length range as the <code>subrange</code> parameter, this method
inserts the elements of <code>newElements</code> at <code>subrange.startIndex</code>. Calling
the <code>insert(contentsOf:at:)</code> method instead is preferred.</p>
<p>Likewise, if you pass a zero-length collection as the <code>newElements</code>
parameter, this method removes the elements in the given subrange
without replacement. Calling the <code>removeSubrange(_:)</code> method instead is
preferred.</p>
<p>Calling this method may invalidate any existing indices for use with this
collection.</p>
<blockquote>
<p>Complexity:
O(<em>n</em> + <em>m</em>), where <em>n</em> is length of this collection and <em>m</em> is the length of <code>newElements</code>. If the call to this method simply appends the contents of <code>newElements</code> to the collection, this method is equivalent to <code>append(contentsOf:)</code>.</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">mutating func replaceSubrange&lt;C>(_ subrange: Range&lt;Self.Index>, with newElements: C) where C: Collection, Self.Element == C.Element</code></div></div><div id="reserveCapacity-f5185bdad5243d37ae23c866665e584b" class="declaration"><a class="toggle-link" href="#comment-reserveCapacity-f5185bdad5243d37ae23c866665e584b">func reserveCapacity(_ n: Int)</a> <span class="required">Required</span><div class="comment collapse in" id="comment-reserveCapacity-f5185bdad5243d37ae23c866665e584b"><p>Prepares the collection to store the specified number of elements, when
doing so is appropriate for the underlying type.</p>
<p>If you are adding a known number of elements to a collection, use this
method to avoid multiple reallocations. A type that conforms to
<code>RangeReplaceableCollection</code> can choose how to respond when this method
is called. Depending on the type, it may make sense to allocate more or
less storage than requested, or to take no action at all.</p>
<ul>
<li>Parameter n: The requested number of elements to store.</li>
</ul>
<h4>Declaration</h4><code class="language-swift">mutating func reserveCapacity(_ n: Int)</code></div></div><h3>Default Implementations</h3><div id="count-e2942a04780e223b215eb8b663cf5353" class="declaration"><a class="toggle-link" href="#comment-count-e2942a04780e223b215eb8b663cf5353">var count</a> <div class="comment collapse in" id="comment-count-e2942a04780e223b215eb8b663cf5353"><p>The number of elements in the collection.</p>
<p>To check whether a collection is empty, use its <code>isEmpty</code> property
instead of comparing <code>count</code> to zero. Unless the collection guarantees
random-access performance, calculating <code>count</code> can be an O(<em>n</em>)
operation.</p>
<blockquote>
<p>Complexity:
O(1) if the collection conforms to <code>RandomAccessCollection</code>; otherwise, O(<em>n</em>), where <em>n</em> is the length of the collection.</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">var count: Int</code></div></div><div id="distance_from_to-a4fda42879a53b11f7add335ae2b09e2" class="declaration"><a class="toggle-link" href="#comment-distance_from_to-a4fda42879a53b11f7add335ae2b09e2">func distance(from start: Self.Index, to end: Self.Index) -> Int</a> <div class="comment collapse in" id="comment-distance_from_to-a4fda42879a53b11f7add335ae2b09e2"><p>Returns the distance between two indices.</p>
<p>Unless the collection conforms to the <code>BidirectionalCollection</code> protocol,
<code>start</code> must be less than or equal to <code>end</code>.</p>
<blockquote>
<p>Complexity:
O(1) if the collection conforms to <code>RandomAccessCollection</code>; otherwise, O(<em>k</em>), where <em>k</em> is the resulting distance.</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">@inlinable public func distance(from start: Self.Index, to end: Self.Index) -> Int</code></div></div><div id="drop_while-ca523aea465630a53d85fd083d49a402" class="declaration"><a class="toggle-link" href="#comment-drop_while-ca523aea465630a53d85fd083d49a402">func drop(while predicate: (Self.Element) throws -> Bool) rethrows -> Self.SubSequence</a> <div class="comment collapse in" id="comment-drop_while-ca523aea465630a53d85fd083d49a402"><p>Returns a subsequence by skipping elements while <code>predicate</code> returns
<code>true</code> and returning the remaining elements.</p>
<ul>
<li>Parameter predicate: A closure that takes an element of the
sequence as its argument and returns <code>true</code> if the element should
be skipped or <code>false</code> if it should be included. Once the predicate
returns <code>false</code> it will not be called again.</li>
</ul>
<blockquote>
<p>Complexity:
O(<em>n</em>), where <em>n</em> is the length of the collection.</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">@inlinable public func drop(while predicate: (Self.Element) throws -> Bool) rethrows -> Self.SubSequence</code></div></div><div id="dropFirst-cfdc97e613e61021a5c6755568f65e2d" class="declaration"><a class="toggle-link" href="#comment-dropFirst-cfdc97e613e61021a5c6755568f65e2d">func dropFirst(_ k: Int = 1) -> Self.SubSequence</a> <div class="comment collapse in" id="comment-dropFirst-cfdc97e613e61021a5c6755568f65e2d"><p>Returns a subsequence containing all but the given number of initial
elements.</p>
<p>If the number of elements to drop exceeds the number of elements in
the collection, the result is an empty subsequence.</p>
<pre><code class="language-swift">let numbers = [1, 2, 3, 4, 5]
print(numbers.dropFirst(2))
// Prints &quot;[3, 4, 5]&quot;
print(numbers.dropFirst(10))
// Prints &quot;[]&quot;
</code></pre>
<ul>
<li>Parameter k: The number of elements to drop from the beginning of
the collection. <code>k</code> must be greater than or equal to zero.</li>
</ul>
<blockquote>
<p>Complexity:
O(1) if the collection conforms to <code>RandomAccessCollection</code>; otherwise, O(<em>k</em>), where <em>k</em> is the number of elements to drop from the beginning of the collection.</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">@inlinable public func dropFirst(_ k: Int = 1) -> Self.SubSequence</code></div></div><div id="dropLast-7c2fee97e997e1ca958d9114f37e2c7f" class="declaration"><a class="toggle-link" href="#comment-dropLast-7c2fee97e997e1ca958d9114f37e2c7f">func dropLast(_ k: Int = 1) -> Self.SubSequence</a> <div class="comment collapse in" id="comment-dropLast-7c2fee97e997e1ca958d9114f37e2c7f"><p>Returns a subsequence containing all but the specified number of final
elements.</p>
<p>If the number of elements to drop exceeds the number of elements in the
collection, the result is an empty subsequence.</p>
<pre><code class="language-swift">let numbers = [1, 2, 3, 4, 5]
print(numbers.dropLast(2))
// Prints &quot;[1, 2, 3]&quot;
print(numbers.dropLast(10))
// Prints &quot;[]&quot;
</code></pre>
<ul>
<li>Parameter k: The number of elements to drop off the end of the
collection. <code>k</code> must be greater than or equal to zero.</li>
</ul>
<blockquote>
<p>Complexity:
O(1) if the collection conforms to <code>RandomAccessCollection</code>; otherwise, O(<em>n</em>), where <em>n</em> is the length of the collection.</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">@inlinable public func dropLast(_ k: Int = 1) -> Self.SubSequence</code></div></div><div id="first-8b04d5e3775d298e78455efc5ca404d5" class="declaration"><a class="toggle-link" href="#comment-first-8b04d5e3775d298e78455efc5ca404d5">var first</a> <div class="comment collapse in" id="comment-first-8b04d5e3775d298e78455efc5ca404d5"><p>The first element of the collection.</p>
<p>If the collection is empty, the value of this property is <code>nil</code>.</p>
<pre><code class="language-swift">let numbers = [10, 20, 30, 40, 50]
if let firstNumber = numbers.first {
    print(firstNumber)
}
// Prints &quot;10&quot;
</code></pre>
<h4>Declaration</h4><code class="language-swift">var first: Self.Element?</code></div></div><div id="firstIndex_where-521d8351894345820bf85f296b29656f" class="declaration"><a class="toggle-link" href="#comment-firstIndex_where-521d8351894345820bf85f296b29656f">func firstIndex(where predicate: (Self.Element) throws -> Bool) rethrows -> Self.Index?</a> <div class="comment collapse in" id="comment-firstIndex_where-521d8351894345820bf85f296b29656f"><p>Returns the first index in which an element of the collection satisfies
the given predicate.</p>
<p>You can use the predicate to find an element of a type that doesn't
conform to the <code>Equatable</code> protocol or to find an element that matches
particular criteria. Here's an example that finds a student name that
begins with the letter &quot;A&quot;:</p>
<pre><code class="language-swift">let students = [&quot;Kofi&quot;, &quot;Abena&quot;, &quot;Peter&quot;, &quot;Kweku&quot;, &quot;Akosua&quot;]
if let i = students.firstIndex(where: { $0.hasPrefix(&quot;A&quot;) }) {
    print(&quot;\(students[i]) starts with 'A'!&quot;)
}
// Prints &quot;Abena starts with 'A'!&quot;
</code></pre>
<ul>
<li>Parameter predicate: A closure that takes an element as its argument
and returns a Boolean value that indicates whether the passed element
represents a match.</li>
</ul>
<blockquote>
<p>Complexity:
O(<em>n</em>), where <em>n</em> is the length of the collection.</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">@inlinable public func firstIndex(where predicate: (Self.Element) throws -> Bool) rethrows -> Self.Index?</code></div></div><div id="formIndex_offsetBy-2182b1026c3b63acf3f08aef9a292825" class="declaration"><a class="toggle-link" href="#comment-formIndex_offsetBy-2182b1026c3b63acf3f08aef9a292825">func formIndex(_ i: inout Self.Index, offsetBy distance: Int)</a> <div class="comment collapse in" id="comment-formIndex_offsetBy-2182b1026c3b63acf3f08aef9a292825"><p>Offsets the given index by the specified distance.</p>
<p>The value passed as <code>distance</code> must not offset <code>i</code> beyond the bounds of
the collection.</p>
<blockquote>
<p>Complexity:
O(1) if the collection conforms to <code>RandomAccessCollection</code>; otherwise, O(<em>k</em>), where <em>k</em> is the absolute value of <code>distance</code>.</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">@inlinable public func formIndex(_ i: inout Self.Index, offsetBy distance: Int)</code></div></div><div id="formIndex_offsetBy_limitedBy-77cef915940f272750243f38458b9a94" class="declaration"><a class="toggle-link" href="#comment-formIndex_offsetBy_limitedBy-77cef915940f272750243f38458b9a94">func formIndex(_ i: inout Self.Index, offsetBy distance: Int, limitedBy limit: Self.Index) -> Bool</a> <div class="comment collapse in" id="comment-formIndex_offsetBy_limitedBy-77cef915940f272750243f38458b9a94"><p>Offsets the given index by the specified distance, or so that it equals
the given limiting index.</p>
<p>The value passed as <code>distance</code> must not offset <code>i</code> beyond the bounds of
the collection, unless the index passed as <code>limit</code> prevents offsetting
beyond those bounds.</p>
<blockquote>
<p>Complexity:
O(1) if the collection conforms to <code>RandomAccessCollection</code>; otherwise, O(<em>k</em>), where <em>k</em> is the absolute value of <code>distance</code>.</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">@inlinable public func formIndex(_ i: inout Self.Index, offsetBy distance: Int, limitedBy limit: Self.Index) -> Bool</code></div></div><div id="formIndex_after-13e63bf648e45549081b5214c49499ce" class="declaration"><a class="toggle-link" href="#comment-formIndex_after-13e63bf648e45549081b5214c49499ce">func formIndex(after i: inout Self.Index)</a> <div class="comment collapse in" id="comment-formIndex_after-13e63bf648e45549081b5214c49499ce"><p>Replaces the given index with its successor.</p>
<ul>
<li>Parameter i: A valid index of the collection. <code>i</code> must be less than
<code>endIndex</code>.</li>
</ul>
<h4>Declaration</h4><code class="language-swift">@inlinable public func formIndex(after i: inout Self.Index)</code></div></div><div id="index_offsetBy-41a458d51308012c8383ec58b39fc85a" class="declaration"><a class="toggle-link" href="#comment-index_offsetBy-41a458d51308012c8383ec58b39fc85a">func index(_ i: Self.Index, offsetBy distance: Int) -> Self.Index</a> <div class="comment collapse in" id="comment-index_offsetBy-41a458d51308012c8383ec58b39fc85a"><p>Returns an index that is the specified distance from the given index.</p>
<p>The following example obtains an index advanced four positions from a
string's starting index and then prints the character at that position.</p>
<pre><code class="language-swift">let s = &quot;Swift&quot;
let i = s.index(s.startIndex, offsetBy: 4)
print(s[i])
// Prints &quot;t&quot;
</code></pre>
<p>The value passed as <code>distance</code> must not offset <code>i</code> beyond the bounds of
the collection.</p>
<blockquote>
<p>Complexity:
O(1) if the collection conforms to <code>RandomAccessCollection</code>; otherwise, O(<em>k</em>), where <em>k</em> is the absolute value of <code>distance</code>.</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">@inlinable public func index(_ i: Self.Index, offsetBy distance: Int) -> Self.Index</code></div></div><div id="index_offsetBy_limitedBy-c29af88d32684042c610f5da0fa07130" class="declaration"><a class="toggle-link" href="#comment-index_offsetBy_limitedBy-c29af88d32684042c610f5da0fa07130">func index(_ i: Self.Index, offsetBy distance: Int, limitedBy limit: Self.Index) -> Self.Index?</a> <div class="comment collapse in" id="comment-index_offsetBy_limitedBy-c29af88d32684042c610f5da0fa07130"><p>Returns an index that is the specified distance from the given index,
unless that distance is beyond a given limiting index.</p>
<p>The following example obtains an index advanced four positions from a
string's starting index and then prints the character at that position.
The operation doesn't require going beyond the limiting <code>s.endIndex</code>
value, so it succeeds.</p>
<pre><code class="language-swift">let s = &quot;Swift&quot;
if let i = s.index(s.startIndex, offsetBy: 4, limitedBy: s.endIndex) {
    print(s[i])
}
// Prints &quot;t&quot;
</code></pre>
<p>The next example attempts to retrieve an index six positions from
<code>s.startIndex</code> but fails, because that distance is beyond the index
passed as <code>limit</code>.</p>
<pre><code class="language-swift">let j = s.index(s.startIndex, offsetBy: 6, limitedBy: s.endIndex)
print(j)
// Prints &quot;nil&quot;
</code></pre>
<p>The value passed as <code>distance</code> must not offset <code>i</code> beyond the bounds of
the collection, unless the index passed as <code>limit</code> prevents offsetting
beyond those bounds.</p>
<blockquote>
<p>Complexity:
O(1) if the collection conforms to <code>RandomAccessCollection</code>; otherwise, O(<em>k</em>), where <em>k</em> is the absolute value of <code>distance</code>.</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">@inlinable public func index(_ i: Self.Index, offsetBy distance: Int, limitedBy limit: Self.Index) -> Self.Index?</code></div></div><div id="isEmpty-371a91e5c91e40f6212ab50995ccb0cd" class="declaration"><a class="toggle-link" href="#comment-isEmpty-371a91e5c91e40f6212ab50995ccb0cd">var isEmpty</a> <div class="comment collapse in" id="comment-isEmpty-371a91e5c91e40f6212ab50995ccb0cd"><p>A Boolean value indicating whether the collection is empty.</p>
<p>When you need to check whether your collection is empty, use the
<code>isEmpty</code> property instead of checking that the <code>count</code> property is
equal to zero. For collections that don't conform to
<code>RandomAccessCollection</code>, accessing the <code>count</code> property iterates
through the elements of the collection.</p>
<pre><code class="language-swift">let horseName = &quot;Silver&quot;
if horseName.isEmpty {
    print(&quot;I've been through the desert on a horse with no name.&quot;)
} else {
    print(&quot;Hi ho, \(horseName)!&quot;)
}
// Prints &quot;Hi ho, Silver!&quot;)
</code></pre>
<blockquote>
<p>Complexity:
O(1)</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">var isEmpty: Bool</code></div></div><div id="map-d86ddaae0a60cc94d2ecc186aad275b0" class="declaration"><a class="toggle-link" href="#comment-map-d86ddaae0a60cc94d2ecc186aad275b0">func map(_ transform: (Self.Element) throws -> T) rethrows -> [T]</a> <div class="comment collapse in" id="comment-map-d86ddaae0a60cc94d2ecc186aad275b0"><p>Returns an array containing the results of mapping the given closure
over the sequence's elements.</p>
<p>In this example, <code>map</code> is used first to convert the names in the array
to lowercase strings and then to count their characters.</p>
<pre><code class="language-swift">let cast = [&quot;Vivien&quot;, &quot;Marlon&quot;, &quot;Kim&quot;, &quot;Karl&quot;]
let lowercaseNames = cast.map { $0.lowercased() }
// 'lowercaseNames' == [&quot;vivien&quot;, &quot;marlon&quot;, &quot;kim&quot;, &quot;karl&quot;]
let letterCounts = cast.map { $0.count }
// 'letterCounts' == [6, 6, 3, 4]
</code></pre>
<ul>
<li>Parameter transform: A mapping closure. <code>transform</code> accepts an
element of this sequence as its parameter and returns a transformed
value of the same or of a different type.</li>
</ul>
<h4>Declaration</h4><code class="language-swift">@inlinable public func map&lt;T>(_ transform: (Self.Element) throws -> T) rethrows -> [T]</code></div></div><div id="prefix-0c90e3e9cf80d963bff973e2da9dc4d7" class="declaration"><a class="toggle-link" href="#comment-prefix-0c90e3e9cf80d963bff973e2da9dc4d7">func prefix(_ maxLength: Int) -> Self.SubSequence</a> <div class="comment collapse in" id="comment-prefix-0c90e3e9cf80d963bff973e2da9dc4d7"><p>Returns a subsequence, up to the specified maximum length, containing
the initial elements of the collection.</p>
<p>If the maximum length exceeds the number of elements in the collection,
the result contains all the elements in the collection.</p>
<pre><code class="language-swift">let numbers = [1, 2, 3, 4, 5]
print(numbers.prefix(2))
// Prints &quot;[1, 2]&quot;
print(numbers.prefix(10))
// Prints &quot;[1, 2, 3, 4, 5]&quot;
</code></pre>
<ul>
<li>Parameter maxLength: The maximum number of elements to return.
<code>maxLength</code> must be greater than or equal to zero.</li>
</ul>
<blockquote>
<p>Complexity:
O(1) if the collection conforms to <code>RandomAccessCollection</code>; otherwise, O(<em>k</em>), where <em>k</em> is the number of elements to select from the beginning of the collection.</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">@inlinable public func prefix(_ maxLength: Int) -> Self.SubSequence</code></div></div><div id="prefix_through-af35e7e2415a9bfae8f02be969ebdc70" class="declaration"><a class="toggle-link" href="#comment-prefix_through-af35e7e2415a9bfae8f02be969ebdc70">func prefix(through position: Self.Index) -> Self.SubSequence</a> <div class="comment collapse in" id="comment-prefix_through-af35e7e2415a9bfae8f02be969ebdc70"><p>Returns a subsequence from the start of the collection through the
specified position.</p>
<p>The resulting subsequence <em>includes</em> the element at the position <code>end</code>.
The following example searches for the index of the number <code>40</code> in an
array of integers, and then prints the prefix of the array up to, and
including, that index:</p>
<pre><code class="language-swift">let numbers = [10, 20, 30, 40, 50, 60]
if let i = numbers.firstIndex(of: 40) {
    print(numbers.prefix(through: i))
}
// Prints &quot;[10, 20, 30, 40]&quot;
</code></pre>
<p>Using the <code>prefix(through:)</code> method is equivalent to using a partial
closed range as the collection's subscript. The subscript notation is
preferred over <code>prefix(through:)</code>.</p>
<pre><code class="language-swift">if let i = numbers.firstIndex(of: 40) {
    print(numbers[...i])
}
// Prints &quot;[10, 20, 30, 40]&quot;
</code></pre>
<ul>
<li>Parameter end: The index of the last element to include in the
resulting subsequence. <code>end</code> must be a valid index of the collection
that is not equal to the <code>endIndex</code> property.</li>
</ul>
<blockquote>
<p>Complexity:
O(1)</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">@inlinable public func prefix(through position: Self.Index) -> Self.SubSequence</code></div></div><div id="prefix_upTo-08fca4e629db666df1579b83a65e98f4" class="declaration"><a class="toggle-link" href="#comment-prefix_upTo-08fca4e629db666df1579b83a65e98f4">func prefix(upTo end: Self.Index) -> Self.SubSequence</a> <div class="comment collapse in" id="comment-prefix_upTo-08fca4e629db666df1579b83a65e98f4"><p>Returns a subsequence from the start of the collection up to, but not
including, the specified position.</p>
<p>The resulting subsequence <em>does not include</em> the element at the position
<code>end</code>. The following example searches for the index of the number <code>40</code>
in an array of integers, and then prints the prefix of the array up to,
but not including, that index:</p>
<pre><code class="language-swift">let numbers = [10, 20, 30, 40, 50, 60]
if let i = numbers.firstIndex(of: 40) {
    print(numbers.prefix(upTo: i))
}
// Prints &quot;[10, 20, 30]&quot;
</code></pre>
<p>Passing the collection's starting index as the <code>end</code> parameter results in
an empty subsequence.</p>
<pre><code class="language-swift">print(numbers.prefix(upTo: numbers.startIndex))
// Prints &quot;[]&quot;
</code></pre>
<p>Using the <code>prefix(upTo:)</code> method is equivalent to using a partial
half-open range as the collection's subscript. The subscript notation is
preferred over <code>prefix(upTo:)</code>.</p>
<pre><code class="language-swift">if let i = numbers.firstIndex(of: 40) {
    print(numbers[..&lt;i])
}
// Prints &quot;[10, 20, 30]&quot;
</code></pre>
<ul>
<li>Parameter end: The &quot;past the end&quot; index of the resulting subsequence.
<code>end</code> must be a valid index of the collection.</li>
</ul>
<blockquote>
<p>Complexity:
O(1)</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">@inlinable public func prefix(upTo end: Self.Index) -> Self.SubSequence</code></div></div><div id="prefix_while-c1b423a2138af25080fc01f86d3d8a66" class="declaration"><a class="toggle-link" href="#comment-prefix_while-c1b423a2138af25080fc01f86d3d8a66">func prefix(while predicate: (Self.Element) throws -> Bool) rethrows -> Self.SubSequence</a> <div class="comment collapse in" id="comment-prefix_while-c1b423a2138af25080fc01f86d3d8a66"><p>Returns a subsequence containing the initial elements until <code>predicate</code>
returns <code>false</code> and skipping the remaining elements.</p>
<ul>
<li>Parameter predicate: A closure that takes an element of the
sequence as its argument and returns <code>true</code> if the element should
be included or <code>false</code> if it should be excluded. Once the predicate
returns <code>false</code> it will not be called again.</li>
</ul>
<blockquote>
<p>Complexity:
O(<em>n</em>), where <em>n</em> is the length of the collection.</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">@inlinable public func prefix(while predicate: (Self.Element) throws -> Bool) rethrows -> Self.SubSequence</code></div></div><div id="randomElement-cb73dc7294d283d83fedcab76a23b81e" class="declaration"><a class="toggle-link" href="#comment-randomElement-cb73dc7294d283d83fedcab76a23b81e">func randomElement() -> Self.Element?</a> <div class="comment collapse in" id="comment-randomElement-cb73dc7294d283d83fedcab76a23b81e"><p>Returns a random element of the collection.</p>
<p>Call <code>randomElement()</code> to select a random element from an array or
another collection. This example picks a name at random from an array:</p>
<pre><code class="language-swift">let names = [&quot;Zoey&quot;, &quot;Chloe&quot;, &quot;Amani&quot;, &quot;Amaia&quot;]
let randomName = names.randomElement()!
// randomName == &quot;Amani&quot;
</code></pre>
<p>This method is equivalent to calling <code>randomElement(using:)</code>, passing in
the system's default random generator.</p>
<blockquote>
<p>Complexity:
O(1) if the collection conforms to <code>RandomAccessCollection</code>; otherwise, O(<em>n</em>), where <em>n</em> is the length of the collection.</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">@inlinable public func randomElement() -> Self.Element?</code></div></div><div id="randomElement_using-dc67acb25c029fcc1df75c2ca154c119" class="declaration"><a class="toggle-link" href="#comment-randomElement_using-dc67acb25c029fcc1df75c2ca154c119">func randomElement(using generator: inout T) -> Self.Element?</a> <div class="comment collapse in" id="comment-randomElement_using-dc67acb25c029fcc1df75c2ca154c119"><p>Returns a random element of the collection, using the given generator as
a source for randomness.</p>
<p>Call <code>randomElement(using:)</code> to select a random element from an array or
another collection when you are using a custom random number generator.
This example picks a name at random from an array:</p>
<pre><code class="language-swift">let names = [&quot;Zoey&quot;, &quot;Chloe&quot;, &quot;Amani&quot;, &quot;Amaia&quot;]
let randomName = names.randomElement(using: &amp;myGenerator)!
// randomName == &quot;Amani&quot;
</code></pre>
<ul>
<li>Parameter generator: The random number generator to use when choosing a
random element.</li>
</ul>
<blockquote>
<p>Complexity:
O(1) if the collection conforms to <code>RandomAccessCollection</code>; otherwise, O(<em>n</em>), where <em>n</em> is the length of the collection.</p>
</blockquote>
<blockquote>
<p>Note:
The algorithm used to select a random element may change in a future version of Swift. If you're passing a generator that results in the same sequence of elements each time you run your program, that sequence may change when your program is compiled using a different version of Swift.</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">@inlinable public func randomElement&lt;T>(using generator: inout T) -> Self.Element? where T: RandomNumberGenerator</code></div></div><div id="split_maxSplits_omittingEmptySubsequences_whereSeparator-c732c52f9b4aaad6daab6945dcdfba81" class="declaration"><a class="toggle-link" href="#comment-split_maxSplits_omittingEmptySubsequences_whereSeparator-c732c52f9b4aaad6daab6945dcdfba81">func split(maxSplits: Int = Int.max, omittingEmptySubsequences: Bool = true, whereSeparator isSeparator: (Self.Element) throws -> Bool) rethrows -> [Self.SubSequence]</a> <div class="comment collapse in" id="comment-split_maxSplits_omittingEmptySubsequences_whereSeparator-c732c52f9b4aaad6daab6945dcdfba81"><p>Returns the longest possible subsequences of the collection, in order,
that don't contain elements satisfying the given predicate.</p>
<p>The resulting array consists of at most <code>maxSplits + 1</code> subsequences.
Elements that are used to split the sequence are not returned as part of
any subsequence.</p>
<p>The following examples show the effects of the <code>maxSplits</code> and
<code>omittingEmptySubsequences</code> parameters when splitting a string using a
closure that matches spaces. The first use of <code>split</code> returns each word
that was originally separated by one or more spaces.</p>
<pre><code class="language-swift">let line = &quot;BLANCHE:   I don't want realism. I want magic!&quot;
print(line.split(whereSeparator: { $0 == &quot; &quot; }))
// Prints &quot;[&quot;BLANCHE:&quot;, &quot;I&quot;, &quot;don\'t&quot;, &quot;want&quot;, &quot;realism.&quot;, &quot;I&quot;, &quot;want&quot;, &quot;magic!&quot;]&quot;
</code></pre>
<p>The second example passes <code>1</code> for the <code>maxSplits</code> parameter, so the
original string is split just once, into two new strings.</p>
<pre><code class="language-swift">print(line.split(maxSplits: 1, whereSeparator: { $0 == &quot; &quot; }))
// Prints &quot;[&quot;BLANCHE:&quot;, &quot;  I don\'t want realism. I want magic!&quot;]&quot;
</code></pre>
<p>The final example passes <code>false</code> for the <code>omittingEmptySubsequences</code>
parameter, so the returned array contains empty strings where spaces
were repeated.</p>
<pre><code class="language-swift">print(line.split(omittingEmptySubsequences: false, whereSeparator: { $0 == &quot; &quot; }))
// Prints &quot;[&quot;BLANCHE:&quot;, &quot;&quot;, &quot;&quot;, &quot;I&quot;, &quot;don\'t&quot;, &quot;want&quot;, &quot;realism.&quot;, &quot;I&quot;, &quot;want&quot;, &quot;magic!&quot;]&quot;
</code></pre>
<blockquote>
<p>Complexity:
O(<em>n</em>), where <em>n</em> is the length of the collection.</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">@inlinable public func split(maxSplits: Int = Int.max, omittingEmptySubsequences: Bool = true, whereSeparator isSeparator: (Self.Element) throws -> Bool) rethrows -> [Self.SubSequence]</code></div></div><div id="subscript_r-466c73e996c8d896f1c6a25f9add8052" class="declaration"><a class="toggle-link" href="#comment-subscript_r-466c73e996c8d896f1c6a25f9add8052">subscript subscript(r:)</a> <div class="comment collapse in" id="comment-subscript_r-466c73e996c8d896f1c6a25f9add8052"><p>Accesses the contiguous subrange of the collection's elements specified
by a range expression.</p>
<p>The range expression is converted to a concrete subrange relative to this
collection. For example, using a <code>PartialRangeFrom</code> range expression
with an array accesses the subrange from the start of the range
expression until the end of the array.</p>
<pre><code class="language-swift">let streets = [&quot;Adams&quot;, &quot;Bryant&quot;, &quot;Channing&quot;, &quot;Douglas&quot;, &quot;Evarts&quot;]
let streetsSlice = streets[2...]
print(streetsSlice)
// [&quot;Channing&quot;, &quot;Douglas&quot;, &quot;Evarts&quot;]
</code></pre>
<p>The accessed slice uses the same indices for the same elements as the
original collection uses. This example searches <code>streetsSlice</code> for one
of the strings in the slice, and then uses that index in the original
array.</p>
<pre><code class="language-swift">let index = streetsSlice.firstIndex(of: &quot;Evarts&quot;)    // 4
print(streets[index!])
// &quot;Evarts&quot;
</code></pre>
<p>Always use the slice's <code>startIndex</code> property instead of assuming that its
indices start at a particular value. Attempting to access an element by
using an index outside the bounds of the slice's indices may result in a
runtime error, even if that index is valid for the original collection.</p>
<pre><code class="language-swift">print(streetsSlice.startIndex)
// 2
print(streetsSlice[2])
// &quot;Channing&quot;

print(streetsSlice[0])
// error: Index out of bounds
</code></pre>
<ul>
<li>Parameter bounds: A range of the collection's indices. The bounds of
the range must be valid indices of the collection.</li>
</ul>
<blockquote>
<p>Complexity:
O(1)</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">@inlinable public subscript&lt;R>(r: R) where R: RangeExpression, Self.Index == R.Bound -> Self.SubSequence</code></div></div><div id="subscript_x-1b251887405ca8ce324c4bcc9f09a96e" class="declaration"><a class="toggle-link" href="#comment-subscript_x-1b251887405ca8ce324c4bcc9f09a96e">subscript subscript(x:)</a> <div class="comment collapse in" id="comment-subscript_x-1b251887405ca8ce324c4bcc9f09a96e"><h4>Declaration</h4><code class="language-swift">@inlinable public subscript(x: (UnboundedRange_) -> ()) -> Self.SubSequence</code></div></div><div id="suffix-f3a8eed76ea35c11a090befc2c217d73" class="declaration"><a class="toggle-link" href="#comment-suffix-f3a8eed76ea35c11a090befc2c217d73">func suffix(_ maxLength: Int) -> Self.SubSequence</a> <div class="comment collapse in" id="comment-suffix-f3a8eed76ea35c11a090befc2c217d73"><p>Returns a subsequence, up to the given maximum length, containing the
final elements of the collection.</p>
<p>If the maximum length exceeds the number of elements in the collection,
the result contains all the elements in the collection.</p>
<pre><code class="language-swift">let numbers = [1, 2, 3, 4, 5]
print(numbers.suffix(2))
// Prints &quot;[4, 5]&quot;
print(numbers.suffix(10))
// Prints &quot;[1, 2, 3, 4, 5]&quot;
</code></pre>
<ul>
<li>Parameter maxLength: The maximum number of elements to return. The
value of <code>maxLength</code> must be greater than or equal to zero.</li>
</ul>
<blockquote>
<p>Complexity:
O(1) if the collection conforms to <code>RandomAccessCollection</code>; otherwise, O(<em>n</em>), where <em>n</em> is the length of the collection.</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">@inlinable public func suffix(_ maxLength: Int) -> Self.SubSequence</code></div></div><div id="suffix_from-b9c1dd3926930fd47109f90d010ae677" class="declaration"><a class="toggle-link" href="#comment-suffix_from-b9c1dd3926930fd47109f90d010ae677">func suffix(from start: Self.Index) -> Self.SubSequence</a> <div class="comment collapse in" id="comment-suffix_from-b9c1dd3926930fd47109f90d010ae677"><p>Returns a subsequence from the specified position to the end of the
collection.</p>
<p>The following example searches for the index of the number <code>40</code> in an
array of integers, and then prints the suffix of the array starting at
that index:</p>
<pre><code class="language-swift">let numbers = [10, 20, 30, 40, 50, 60]
if let i = numbers.firstIndex(of: 40) {
    print(numbers.suffix(from: i))
}
// Prints &quot;[40, 50, 60]&quot;
</code></pre>
<p>Passing the collection's <code>endIndex</code> as the <code>start</code> parameter results in
an empty subsequence.</p>
<pre><code class="language-swift">print(numbers.suffix(from: numbers.endIndex))
// Prints &quot;[]&quot;
</code></pre>
<p>Using the <code>suffix(from:)</code> method is equivalent to using a partial range
from the index as the collection's subscript. The subscript notation is
preferred over <code>suffix(from:)</code>.</p>
<pre><code class="language-swift">if let i = numbers.firstIndex(of: 40) {
    print(numbers[i...])
}
// Prints &quot;[40, 50, 60]&quot;
</code></pre>
<ul>
<li>Parameter start: The index at which to start the resulting subsequence.
<code>start</code> must be a valid index of the collection.</li>
</ul>
<blockquote>
<p>Complexity:
O(1)</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">@inlinable public func suffix(from start: Self.Index) -> Self.SubSequence</code></div></div><div id="underestimatedCount-cea0d409be772f5ea3e5db1bcf473531" class="declaration"><a class="toggle-link" href="#comment-underestimatedCount-cea0d409be772f5ea3e5db1bcf473531">var underestimatedCount</a> <div class="comment collapse in" id="comment-underestimatedCount-cea0d409be772f5ea3e5db1bcf473531"><p>A value less than or equal to the number of elements in the collection.</p>
<blockquote>
<p>Complexity:
O(1) if the collection conforms to <code>RandomAccessCollection</code>; otherwise, O(<em>n</em>), where <em>n</em> is the length of the collection.</p>
</blockquote>
<h4>Declaration</h4><code class="language-swift">var underestimatedCount: Int</code></div></div>